<template>
  <div v-loading="loading" class="flowTable-container">
    <snack-line
      :id="id"
      :data="list"
      :tenant-id="tenantId"
      :email="email"
      @deleteSign="deleteSign"
    />
  </div>
</template>

<script>
// import Cascader from '@/components/Cascader'
import { getHistoryList, getProcessTasks, taskRemoveSign } from '@/api/approval'
import '@/styles/flow.scss' // 引入样式表
import SnackLine from './components/snackLine'
export default {
  name: 'ApprovalHistory',
  components: {
    SnackLine
  },
  data() {
    return {
      loading: false,
      id: this.$route.query.id,
      tenantId: this.$route.query.tenantId,
      flag: this.$route.query.flag,
      list: [],
      email: this.$route.query.email
    }
  },
  watch: {
    $route: {
      deep: true,
      handler() {
        if (this.flag === 'processTasks') {
          this.getProcessTasks(this.id, this.tenantId)
        } else {
          this.id && this.getHistoryList(this.id, this.tenantId)
        }
      }
    }
  },
  created() {
    if (this.flag === 'processTasks') {
      this.getProcessTasks(this.id, this.tenantId)
    } else {
      this.id && this.getHistoryList(this.id, this.tenantId)
    }
  },
  methods: {
    async getHistoryList(id, tenantId) {
      this.loading = true
      const { records } = await getHistoryList({
        condition: { businessKey: id, tenantId }
      })
      this.list = records
      this.loading = false
      console.log(records)
    },
    // 加签工作流列表
    async getProcessTasks(id, tenantId) {
      this.loading = true
      const { records } = await getProcessTasks(id, {
        tenantId
      })
      this.list = records
      this.loading = false
    },
    deleteSign(val) {
      const params = {
        businessKey: val.businessKey,
        assignee: this.email
        // assignee: 'tianziwei@itcast.cn'
      }
      const data = {
        tenantId: val.tenantId,
        taskDefinitionKey: val.taskDefinitionKey
      }
      this.$confirm('删除审批节?', '删除审批节', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const success = await taskRemoveSign(params, data)
          if (success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getProcessTasks(this.id, this.tenantId)
          }
        })
        .catch(() => {})
    }
  }
}
</script>
